package com.orangelife.service.propFeeComputeRule.model;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.orangelife.utils.StringUtils;

public class Ratio implements ISelfFormula {
public static void main(String[] args) {
	int m=1;
	int n=5;
	System.out.println(Double.parseDouble("2.39")>1.39);
    System.out.println(m/n+(m%n==0?0:1));
	
	List<Map<String,String>> m1=new ArrayList<>();
	Map<String,String> pp=new HashMap<String,String>();
	pp.put("start","0");
	pp.put("end","50");
	pp.put("ratio","1");
	Map<String,String> pp1=new HashMap<String,String>();
	pp1.put("start","50");
	pp1.put("end","100");
	pp1.put("ratio","2");
	Map<String,String> pp2=new HashMap<String,String>();
	pp2.put("start","100");
	pp2.put("end","");
	pp2.put("ratio","6.2");
	m1.add(pp);
	m1.add(pp1);
    m1.add(pp2);
	
	Ratio r1=new Ratio("4",m1);
	
	
	System.out.println(r1.getRario("110")+"&*&*&*");
	System.out.println(6/5+"**"+6%5);
	
	Ratio r=new Ratio("1","1",7,"0.2",1,"1",0);
	

	System.out.println(r.getFloorRatio(6)+"^^^^^^^^");
}
	private BigDecimal initNumber;
	private int initFloor;
	private BigDecimal addUnitNumber;
	private BigDecimal subUnitNumber;
	private int addUnitClass;
	private int subUnitClass;
	private String ratioType;	
	private List<Map<String,String>> ratios;
     private List<Map<String,Double>> ratios2;
     private List<Map<String,String>> ratios3;
	private Map<Integer,BigDecimal> ratiosParam;
	public Ratio(String ratioType,String initNumber,int initFloor,String addUnitNumber,int addUnitClass,String subUnitNumber,int subUnitClass)
	{
		this.ratioType=ratioType;
		this.initNumber=new BigDecimal(initNumber);
		this.initFloor=initFloor;
		this.addUnitNumber=new BigDecimal(addUnitNumber);
		this.addUnitClass=addUnitClass;
		this.subUnitNumber=new BigDecimal(subUnitNumber);
		this.subUnitClass=subUnitClass;
		
	}
	
	public Ratio(){
		
		
		
	};
	
	
	
	public Ratio(String ratioType,List<Map<String,String>> ratios)
	{
		this.ratioType=ratioType;
	    this.ratios=ratios;
	    
	    if(	"2".equals(this.ratioType)){
	    	
	    	   ratiosParam=new HashMap<Integer,BigDecimal>();
	   	    
	   	    for (Map<String, String> map : ratios) {
	   	    	
	   	    	int start=Integer.parseInt(map.get("start"));
	   	    	int end=Integer.parseInt(map.get("end"));
	   	    	

	   	    	for(;start<=end;start++){
	   	    		
	   	    		ratiosParam.put(start, new BigDecimal(map.get("ratio")));
	   	    		
	   	    	}
	   	    
	   	    	
	   	    	
	   	    	
	   		}
	    	
	    }
	    
	    else if("3".equals(this.ratioType)){
	    	
	    this.ratios2=new ArrayList<>();
	
	   	    for (Map<String, String> map : ratios) {
	   	    	
	   	    	
	   	    	Map<String,Double> m=new HashMap<String,Double>();
	   	    	
	   	    	if(StringUtils.isDouble(map.get("start"))&&StringUtils.isDouble(map.get("end"))&&StringUtils.isDouble(map.get("ratio"))){
	   	    	m.put("start", Double.parseDouble(map.get("start")));
	   	    	m.put("end", Double.parseDouble(map.get("end")));
	   	    	m.put("ratio", Double.parseDouble(map.get("ratio")));
//	   	    	double start=Double.parseDouble(map.get("start"));
//	   	    	double end=Double.parseDouble(map.get("end"));
	   	    	
	   	    	ratios2.add(m);
	   	    	}
	   	    	
	   		}
	    	
	    	
	    	
	    }
	    else if("4".equals(this.ratioType)){
	    	
		    this.ratios3=new ArrayList<>();
		
		   	    for (Map<String, String> map : ratios) {
		   	    	
		   	    	
		   	    	Map<String,String> m=new HashMap<String,String>();
		   	    	
		   	    	if(StringUtils.isDouble(map.get("start"))&&StringUtils.isDouble(map.get("ratio"))){
		   	    		
		   	    		if(!StringUtils.isBlank(map.get("end"))){
		   	    			
		   	    			if(!StringUtils.isDouble(map.get("end"))){
		   	    				
		   	    				continue;
		   	    				
		   	    			}
		   	    			
		   	    		}
		   	    		
		   	    		
		   	    	m.put("start", map.get("start"));
		   	    	m.put("end", map.get("end"));
		   	    	m.put("ratio", map.get("ratio"));
//		   	
		   	    	
		   	    	ratios3.add(m);
		   	    	}
		   	    	
		   		}
		    	
		    	
		    	
		    }
	 
		
	}
	
	
	
	
	public  int checkSumRatio(List<Map<String,Object>> floors){
		int n=-10000000;
		for (Map<String, Object> map : floors) {
			
			int	floor=Integer.parseInt(map.get("floor").toString());
			
			if(this.getFloorRatio(floor)==null){
				
				
				return floor;
			};
		}
		
		return n;
		
		
	}
	/**
	 * 
	 * @param floors 格式 List Map  floor floorNum  
	 * */
	public String getSumRatio(List<Map<String,Object>> floors){
		
		BigDecimal n=BigDecimal.ZERO;
		int floor=0;
		int floorNum=0;
		int m=0;
		int p=1;
		
		 
		for (Map<String, Object> map : floors) {
			
			
			
			
			
			floor=Integer.parseInt(map.get("floor").toString());
			floorNum=Integer.parseInt(map.get("floorNum").toString());
		if("1".equals(this.ratioType)){
			
			if(floor>=initFloor){
				
				m=floor-initFloor+1;
                if(this.addUnitClass==0){
					
					p=0;
				}
                else{
				p=m/this.addUnitClass+(m%this.addUnitClass==0?0:1)-1;
				
				}
				n=n.add(initNumber.add(this.addUnitNumber.multiply(new BigDecimal(p))).multiply(new BigDecimal(floorNum)));	
				
			}
					
			if(floor<initFloor){
				
				m=initFloor-floor+1;
               if(this.subUnitClass==0){
					
					p=0;
				}
               else{
				p=m/this.subUnitClass+(m%this.subUnitClass==0?0:1)-1;
				}
				
				n=n.add(initNumber.subtract(this.subUnitNumber.multiply(new BigDecimal(p))).multiply(new BigDecimal(floorNum)));	
				
			}
			
			
		}else if("2".equals(this.ratioType)){
			
			
			
			n=n.add(this.ratiosParam.get(floor).multiply(new BigDecimal(floorNum)));
			
			
			
		}
			
				
			
		}
		
		
		
		
		return n.toString();
		
	}
	public String getRario(String val){
		
		BigDecimal sum=BigDecimal.ZERO;
		BigDecimal n=BigDecimal.ZERO;
		BigDecimal x=new BigDecimal(val);
		Double d=Double.parseDouble(val);
		if("3".equals(this.ratioType)){
			
			for (Map<String,Double> p : ratios2) {
				
		         if(d>=p.get("start")&&d<=p.get("end")){
		        	 
		        	 
		        	 return p.get("ratio").toString();
		         }
					
				}
			
		}
		else if("4".equals(this.ratioType)){
			
			
			for (Map<String,String> p : ratios3) {
				
				if(x.compareTo(new BigDecimal(p.get("start")))<=0){
					
				continue;
					
				}
				
				
				
				
				if(!StringUtils.isBlank(p.get("end").toString())&&x.compareTo(new BigDecimal(p.get("end")))>0){
					n=new BigDecimal(p.get("end")).subtract(new BigDecimal(p.get("start")));	
					
					
				}else{
					
				
					n=x.subtract(new BigDecimal(p.get("start")));
				}
				
			
				
				
				sum=	sum.add(new BigDecimal(p.get("ratio").toString()).multiply(n));
//		         if(d>=p.get("start")&&d<=p.get("end")){
//		        	 
//		        	 
//		        	 return p.get("ratio").toString();
//		         }
					
				}
			
			return sum.toString();
			
			
		}
		
		
		
		return null;
		
		
		
	}
	public String getFloorRatio(Integer floor){
		
		BigDecimal n=BigDecimal.ZERO;

	
		int m=0;
		int p=1;
		
		try{
		
if("1".equals(this.ratioType)){
	
	if(floor>=initFloor){
		
		m=floor-initFloor+1;
        if(this.addUnitClass==0){
			
			p=0;
		}
     else {
			p=m/this.addUnitClass+(m%this.addUnitClass==0?0:1)-1;
		}
		
		
      n=initNumber.add(this.addUnitNumber.multiply(new BigDecimal(p)));				
	}
			
	if(floor<initFloor){
		
		m=initFloor-floor+1;
		
		if(this.subUnitClass==0){
			
			p=0;
		}else{
			p=m/this.subUnitClass+(m%this.subUnitClass==0?0:1)-1;

		}
		
		 n=this.initNumber.subtract(this.subUnitNumber.multiply(new BigDecimal(p)));
		
	}
       }else if("2".equals(this.ratioType)){
	
         n=this.ratiosParam.get(floor);
	
	
	
                     }

		}catch(Exception e){
			
			
			return null;
			
		}
  	if(n==null){
  		
  		
  		return null;
  	}

		return n.toString();
		
	}
	public BigDecimal getInitNumber() {
		return initNumber;
	}
	public void setInitNumber(BigDecimal initNumber) {
		this.initNumber = initNumber;
	}
	public BigDecimal getAddUnitNumber() {
		return addUnitNumber;
	}
	public void setAddUnitNumber(BigDecimal addUnitNumber) {
		this.addUnitNumber = addUnitNumber;
	}
	public int getInitFloor() {
		return initFloor;
	}
	public void setInitFloor(int initFloor) {
		this.initFloor = initFloor;
	}
	public BigDecimal getSubUnitNumber() {
		return subUnitNumber;
	}
	public void setSubUnitNumber(BigDecimal subUnitNumber) {
		this.subUnitNumber = subUnitNumber;
	}
	public int getAddUnitClass() {
		return addUnitClass;
	}
	public void setAddUnitClass(int addUnitClass) {
		this.addUnitClass = addUnitClass;
	}
	public int getSubUnitClass() {
		return subUnitClass;
	}
	public void setSubUnitClass(int subUnitClass) {
		this.subUnitClass = subUnitClass;
	}
	public String getRatioType() {
		return ratioType;
	}
	public void setRatioType(String ratioType) {
		this.ratioType = ratioType;
	}
	public List<Map<String,String>> getRatios() {
		return ratios;
	}
	public void setRatios(List<Map<String,String>> ratios) {
		this.ratios = ratios;
	}
	public Map<Integer, BigDecimal> getRatiosParam() {
		return ratiosParam;
	}
	public void setRatiosParam(Map<Integer, BigDecimal> ratiosParam) {
		this.ratiosParam = ratiosParam;
	}
	
	
	
	@Override
	public BigDecimal complete() {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	
	
}
